package edu.uclmgmu.bacteriosec.aspects.bufferedreader;

import java.io.BufferedReader;

import edu.uclmgmu.bacteriosec.aspects.common.Store;

privileged aspect BufferedReader_IMInverter {

	String around(java.io.BufferedReader f) : target(f) && args() &&  call(* BufferedReader.readLine()) {
		int numberOfCurrentCall=Store.get(thisJoinPointStaticPart.getSignature());
		int numberOfCallInTurn=Store.getNumberOfCallInTurn();
		if (numberOfCurrentCall==numberOfCallInTurn) {
			Store.log(this.getClass().getCanonicalName(), numberOfCurrentCall, thisJoinPointStaticPart.getSignature());
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			return invert(proceed(f));
		} else {
			Store.put(thisJoinPointStaticPart.getSignature(), ++numberOfCurrentCall);
			return proceed(f);
		}
	}
	
	private String invert(Object r) {
		if (r==null)
			return null;
		String sr=r.toString();
		String result=new String();
		for (int i=sr.length()-1; i>=0; i--)
			result+=sr.charAt(i);
		return result;
	}
}
